import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import {HttpService} from '@kylin/http';
import {CascaderService} from './cascader.service';
import {Level, LevelControl} from './interface';

@Component({
    selector: 'kylin-cascader-selector',
    templateUrl: './cascader.component.html'
})

export class CascaderComponent implements OnInit {
    levelControl: LevelControl;
    levelList: Level[];
    @Input() type = 'areaCountry';
    @Input() options: Level[];
    @Input() klStyle: any;
    @Input() ngModel: any[] = [];
    @Output() ngModelChange = new EventEmitter<any>();

    constructor(private http: HttpService, private casecaderSvc: CascaderService) {
    }

    ngOnInit() {
        this.levelList = this.genLevels();
        this.levelControl = new LevelControl(this.levelList, this.http, this.options);
        this.levelControl.start();
    }

    genLevels() {
        return this.casecaderSvc.genLevels((this.type as any), this.options);
    }

    updateNgModel(index) {
        this.ngModel[index] = this.levelControl.levels[index].selectedValue;
        this.ngModelChange.emit(this.ngModel);
    }

    countVisibleLevel() {
        let count = 0;
        this.levelList.map(level => level.hidden ? '' : count++);
        return count;
    }
}
